package com.quantsmart.springsecurityplus.controller;

import com.quantsmart.springsecurityplus.entity.MyMenu;
import com.quantsmart.springsecurityplus.service.MenuService;
import com.quantsmart.springsecurityplus.utils.Result;
import com.quantsmart.springsecurityplus.utils.ResultCode;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @author codermy
 * @createTime 2020/7/10
 */
@Controller
@RequestMapping("/api/menu")
@Api(tags = "系统:菜单管理")
public class MenuController {
    @Autowired
    private MenuService menuService;

    @GetMapping
    @ResponseBody
    @ApiOperation(value = "菜单列表")
    public Result getMenuAll(String queryName,Integer queryType){ //这里没选择接收json字符串，前端传参通过/api/menu？queryName=测试的方式
        return Result.ok().data(menuService.getMenuAll(queryName, queryType)).code(ResultCode.TABLE_SUCCESS);
    }

    @GetMapping(value = "/edit")
    @ApiOperation(value = "跳转修改菜单页面")
    public String editPermission(Model model, MyMenu myMenu) {
        model.addAttribute("myMenu",menuService.getMenuById(myMenu.getId()));
        return "system/menu/menu-edit";
    }

    @PutMapping
    @ResponseBody
    @ApiOperation(value = "修改菜单")
    public Result updateMenu(@RequestBody MyMenu menu) {
        return menuService.updateMenu(menu);
    }


    @GetMapping(value = "/add")
    @ApiOperation(value = "跳转添加菜单页面")
    public String addMenu(Model model) {
        model.addAttribute("myMenu",new MyMenu());
        return "system/menu/menu-add";
    }

    @PostMapping
    @ResponseBody
    @ApiOperation(value = "添加菜单")
    public Result<MyMenu> savePermission(@RequestBody MyMenu myMenu) {
        return menuService.save(myMenu);
    }

    //todo 批量删除
    @DeleteMapping
    @ResponseBody
    @ApiOperation(value = "删除菜单")
    public Result deleteMenu(Integer id) {
        return menuService.delete(id);
    }
}
